package org.trade.user.service.impl;

import java.math.BigDecimal;
import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.trade.common.constant.TradeEnums;
import org.trade.common.protocol.user.ChangeUserMoneyReq;
import org.trade.common.protocol.user.ChangeUserMoneyRes;
import org.trade.common.protocol.user.QueryUserReq;
import org.trade.common.protocol.user.QueryUserRes;
import org.trade.entity.TradeUser;
import org.trade.entity.TradeUserMoneyLog;
import org.trade.entity.TradeUserMoneyLogExample;
import org.trade.mapper.TradeUserMapper;
import org.trade.mapper.TradeUserMoneyLogMapper;
import org.trade.user.service.IUserService;

@Service
public class UserServiceImpl implements IUserService {
	@Autowired
	private TradeUserMapper tradeUserMapper;
	@Autowired
	private TradeUserMoneyLogMapper tradeUserMoneyLogMapper;

	public QueryUserRes queryUser(QueryUserReq queryUserReq) {
		QueryUserRes qrueryUserRes = new QueryUserRes();
		qrueryUserRes.setRetCode(TradeEnums.RetEnum.SUCCESS.getCode());
		qrueryUserRes.setRetInfo(TradeEnums.RetEnum.SUCCESS.getDesc());
		try {
			if (queryUserReq == null || queryUserReq.getUserId() == null) {
				throw new RuntimeException("请求参数不正确");
			}
			TradeUser tradeUser = tradeUserMapper.selectByPrimaryKey(queryUserReq.getUserId());
			if (tradeUser != null) {
				BeanUtils.copyProperties(tradeUser, qrueryUserRes);
			} else {
				throw new RuntimeException("未查询到该用户");
			}
		} catch (Exception ex) {
			qrueryUserRes.setRetCode(TradeEnums.RetEnum.FAIL.getCode());
			qrueryUserRes.setRetInfo(ex.getMessage());
		}
		return qrueryUserRes;
	}

	@Transactional
	public ChangeUserMoneyRes changeUserMoney(ChangeUserMoneyReq changeUserMoneyReq) {
		ChangeUserMoneyRes changeUserMoneyRes = new ChangeUserMoneyRes();
		changeUserMoneyRes.setRetCode(TradeEnums.RetEnum.SUCCESS.getCode());
		changeUserMoneyRes.setRetInfo(TradeEnums.RetEnum.SUCCESS.getDesc());
		if (changeUserMoneyReq == null || changeUserMoneyReq.getUserId() == null
				|| changeUserMoneyReq.getUserMoney() == null) {
			throw new RuntimeException("请求参数不一致");
		}
		if (changeUserMoneyReq.getUserMoney().compareTo(BigDecimal.ZERO) <= 0) {
			throw new RuntimeException("余额不能小于0");
		}
		TradeUserMoneyLog tradeUserMoneyLog = new TradeUserMoneyLog();
		tradeUserMoneyLog.setOrderId(changeUserMoneyReq.getOrderId());
		tradeUserMoneyLog.setUserId(changeUserMoneyReq.getUserId());
		tradeUserMoneyLog.setUseMoney(changeUserMoneyReq.getUserMoney());
		tradeUserMoneyLog.setCreateTime(new Date());
		tradeUserMoneyLog.setMoneyLogType(changeUserMoneyReq.getMoneyLogType());

		TradeUser tradeUser = new TradeUser();
		tradeUser.setUserId(changeUserMoneyReq.getUserId());
		tradeUser.setUserMoney(changeUserMoneyReq.getUserMoney());
		// 查询是否有订单付款记录
		TradeUserMoneyLogExample tradeUserMoneyLogExample = new TradeUserMoneyLogExample();
		tradeUserMoneyLogExample.createCriteria().andUserIdEqualTo(changeUserMoneyReq.getUserId())
				.andOrderIdEqualTo(changeUserMoneyReq.getOrderId())
				.andMoneyLogTypeEqualTo(TradeEnums.UserMoneyLogTypeEnum.PAID.getCode());
		long count = this.tradeUserMoneyLogMapper.countByExample(tradeUserMoneyLogExample);
		// 订单付款
		if (changeUserMoneyReq.getMoneyLogType().equals(TradeEnums.UserMoneyLogTypeEnum.PAID.getCode())) {
			if (count > 0) {
				throw new RuntimeException("已经付过款了，不能再付款");
			}
			tradeUserMapper.reduceUserMoney(tradeUser);
		}
		// 订单退款
		if (changeUserMoneyReq.getMoneyLogType().equals(TradeEnums.UserMoneyLogTypeEnum.PAID.getCode())) {
			if (count == 0) {
				throw new RuntimeException("没有付款信息，不能退款");
			}
			// 防止多次退款
			tradeUserMoneyLogExample = new TradeUserMoneyLogExample();
			tradeUserMoneyLogExample.createCriteria().andUserIdEqualTo(changeUserMoneyReq.getUserId())
					.andOrderIdEqualTo(changeUserMoneyReq.getOrderId())
					.andMoneyLogTypeEqualTo(TradeEnums.UserMoneyLogTypeEnum.REFUND.getCode());
			count = this.tradeUserMoneyLogMapper.countByExample(tradeUserMoneyLogExample);
			if (count > 0) {
				throw new RuntimeException("已经退过款了，不能退款");
			}
			tradeUserMapper.addUserMoney(tradeUser);
		}
		return changeUserMoneyRes;
	}

}
